<?php 

/*
Plugin Name: Work Order Tracking
Description: Plugin para cadastro, administração e consulta de ordens de serviços.
*/

define( 'WP_DEBUG', true );

$nome_plugin = "WOTracking"; 

add_action('init', 'myStartSession', 1);
add_action('wp_logout', 'myEndSession');
add_action('wp_login', 'myEndSession');

function myStartSession() {
    if(!session_id()) {
        session_start();
    }
}

function myEndSession() {
    session_destroy ();
}

// MENU ADMIN

	add_action( 'admin_menu', function() {
		
		# MENU PAGE
		add_menu_page( 'WOTracking', 'WOTracking', 'add_users', 'wot_default', function() {
		echo "<h2>Principal</h2>";
		printf( '<a href="%s">%s</a>', admin_url('admin.php?page=wot_teste' ) , __( 'OK' ));
		} );
		
		
		# SUBMENU PAGES
		add_submenu_page( 'wot_default', 'WOTracking', 'Principal', 'add_users', 'wot_default' );
		add_submenu_page( 'wot_default', 'Criar Nova OS', 'Nova OS', 'add_users', 'criar_nova_os', 'wot_nova_os' );
		add_submenu_page( 'wot_default', 'Localizar / Editar', 'Localizar OS', 'add_users', 'localizar_os', 'wot_localizar_os' );
		add_submenu_page( 'wot_default', 'Configurações', 'Configurações', 'add_users', 'wot_configuracoes', 'wot_configuracoes' );
		add_submenu_page( 'wot_default', 'Configurações', 'Configurações', 'add_users', 'wot_configuracoes1', 'wot_configuracoes1' );
		add_submenu_page( 'wot_default', 'Ordem de Serviço', 'OS Gerada', 'add_users', 'wot_teste', 'wot_teste' );
		add_submenu_page( 'wot_default', 'Orçamento', 'Teste Pagina', 'add_users', 'wot_orcamento_os', 'wot_orcamento_os' );
		
		/*
		# SUBPAGINAS 
		$teste_funcao = add_submenu_page( null, 'OS Gerada', 'OS Gerada', 'add_users', 'wot_teste', 'wot_teste' );
		$teste_funcao2 = add_submenu_page( null, 'WOTracking', 'Teste Pagina', 'add_users', 'wot_orcamento_os', 'wot_orcamento_os' );
		
		add_action( "admin_footer-$teste_funcao", function()
		{
        // INCLUIR NA PAGINA DE SCRIPT
		echo <<<HTML
		<script type="text/javascript">
		jQuery(document).ready( function($) {
		$('#toplevel_page_wot_default')
        .removeClass('wp-has-submenu wp-not-current-submenu menu-top toplevel_page_nextgen-gallery menu-top-first')
        .addClass('wp-has-submenu wp-has-current-submenu wp-menu-open menu-top menu-icon-generic toplevel_page_wot_default menu-top-last')
		.removeClass('wp-not-current-submenu')
        .addClass('current');
		$('')
		});     
		</script>
HTML;

		}); */
	
	});

// PAGINAS EXTERNAS

include( 'pages/criar-nova-os1 ---- click insert.php' );
include( 'pages/os-gerada2 ---- click insert.php' );
include( 'pages/administrar-os.php' );
include( 'pages/configuracoes.php' );
include( 'pages/orcamento-os.php' );
include( 'css/mediaprint.php' );

// CSS E JS

function wot_css_and_js() {
	wp_register_style('wot_css_and_js', plugins_url('css/style.css',__FILE__ ));
	wp_enqueue_style('wot_css_and_js');
	wp_register_script( 'wot_css_and_js', plugins_url('my-admin.js',__FILE__ ));
	wp_enqueue_script('wot_css_and_js');
}

add_action( 'admin_init','wot_css_and_js');

/* function wot_css_print() {
	wp_register_style('wot_css_print', plugins_url('css/print.css',__FILE__ ), false, null, 'print');
	wp_enqueue_style('wot_css_print'); 
}
add_action( 'admin_init','wot_css_print'); */


/* Add stylesheet to the page */

	 add_action( 'admin_enqueue_scripts', 'safely_add_stylesheet_to_admin' );

    
    function safely_add_stylesheet_to_admin( $plugin_page ) {
        
		global $plugin_page;
		
		if( 'wot_teste' != $plugin_page )
        {
             return;
        }
	
        wp_enqueue_style( 'prefix-style', plugins_url('css/print.css', __FILE__), false, null, 'print' );
    }




// CRIAR TABELA

register_activation_hook( __FILE__, 'wot_create_plugin_tables' );

$table_name = $wpdb->prefix . 'table_clients';       # Era dentro da função

function wot_create_plugin_tables()
{
    global $wpdb, $table_name;

    $sql = "CREATE TABLE $table_name (
      id int(11) NOT NULL AUTO_INCREMENT,
      name varchar(255) DEFAULT NULL,
	  email varchar(255) DEFAULT NULL,
      UNIQUE KEY id (id)
    );";

    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta( $sql );
}


register_activation_hook( __FILE__, 'wot_criar_tabela_empresa' );   

$tabela_empresa = $wpdb->prefix . 'tab_empresa';  

function wot_criar_tabela_empresa()
{
    global $wpdb, $tabela_empresa;
    
	$sql = "CREATE TABLE $tabela_empresa (
      id int(11) NOT NULL AUTO_INCREMENT,
      nome_empresa varchar(100) DEFAULT NULL,
	  endereco_empresa varchar(255) DEFAULT NULL,
	  telefone_empresa varchar(100) DEFAULT NULL,
	  email_empresa varchar(100) DEFAULT NULL,
	  site_empresa varchar(100) DEFAULT NULL,
      UNIQUE KEY id (id)
    );";

    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta( $sql );
}
	
	
// EXEMPLO

/* add_action('admin_menu', function() 
{
    # Main page
    add_menu_page( 
        'Vsa', 
        'Vsa', 
        'add_users', // Capability, not role
        'listaj-narocila', 
        function(){ 
            printf(
                '<h2>%s</h2><a href="%s">%s</a>',
                __( 'Main page' ),
                admin_url( 'admin.php?page=single-norcilo&id='.rand(1,25) ),
                __( 'Hidden sub page' )
            );
        },
        'http://sstatic.net/stackexchange/img/favicon.ico'
    );  

    # Child page    
    $hook = add_submenu_page(
        null,
        'Norcilo',
        'Norcilo',
        'add_users',
        'single-norcilo',
        function(){ 
            printf(
                '<h2>%s</h2><a href="%s">%s</a>',
                __( 'Hidden sub page' ),
                admin_url( 'admin.php?page=listaj-narocila' ),
                __( 'back' )
            );
        }
    );

    # Enqueue script in submenu page to fix the current menu indicator
    add_action( "admin_footer-$hook", function()
    {
        echo <<<HTML
<script type="text/javascript">
jQuery(document).ready( function($) {
    $('#toplevel_page_listaj-narocila')
        .removeClass('wp-not-current-submenu')
        .addClass('current');
});     
</script>
HTML;

    });
}); */
	

function wp_gear_manager_admin_scripts() {
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_enqueue_script('jquery');
}

function wp_gear_manager_admin_styles() {
wp_enqueue_style('thickbox');
}

add_action('admin_print_scripts', 'wp_gear_manager_admin_scripts');
add_action('admin_print_styles', 'wp_gear_manager_admin_styles');


add_action('admin_enqueue_scripts', 'my_admin_scripts');
 
function my_admin_scripts() {
    if (isset($_GET['page']) && $_GET['page'] == 'wot_configuracoes') {
        wp_enqueue_media();
        wp_register_script('my-admin-js', WP_PLUGIN_URL.'my-admin.js', array('jquery'));
        wp_enqueue_script('my-admin-js');
    }
}
	
	?>
	